﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MvcBlog.Common.Models
{
    public class CommentDataAccess
    {

        private MvcBlogEntities db;

        public CommentDataAccess(Object db_access)
        {
            db = (MvcBlogEntities)db_access;
        }


        //hier die abfrage-methoden
        public List<Comment> getCommentsByEntryID(int id)
        {
            var res = db.Comment
                       .Where(c => c.FK_Entry_ID == id)
                       .OrderByDescending(c => c.CreateDate);

            return res.ToList();

        }

        public void insertComment(Comment newComment, int Entry_ID, User user)
        {
            DateTime CreateDate = DateTime.Now;
            newComment.CreateDate = CreateDate;

            //eingeloggten User prüfen, Berechtigt?
            //int User_ID = 1;

            //neuen kommentar mit benutzer und eintrag verknüpfen -> eintrag wird implizit gespeichert
            newComment.User = db.User.Single(u => u.User_ID == user.User_ID);
            newComment.Entry = db.Entry.Single(e => e.Entry_ID == Entry_ID);

            //db.Question.AddObject(question);
            db.SaveChanges();
        }

    }
}
